package com.antaohua.platfrom.framework.web.webconfig;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.antaohua.platfrom.framework.web.converter.JsonHttpMessageConverter;


@SuppressWarnings("deprecation")
@Configuration
@EnableWebMvc
@ComponentScans({
	@ComponentScan(basePackages={"com.antaohua.platfrom.**.**.controller"},useDefaultFilters=false,includeFilters={@Filter(type = FilterType.ANNOTATION,value = Controller.class)}),
	@ComponentScan(basePackages={"com.antaohua.platfrom.**.controller"},useDefaultFilters=false,includeFilters={@Filter(type = FilterType.ANNOTATION,value = Controller.class)})
})
public class DispatcherConfig  extends WebMvcConfigurerAdapter{

	
	
	public FastJsonConfig fastJsonConfig() {
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
				SerializerFeature.WriteMapNullValue,
				SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.WriteNullListAsEmpty,
				SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteNullBooleanAsFalse,
				SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteMapNullValue,
				SerializerFeature.WriteDateUseDateFormat,
				SerializerFeature.BrowserCompatible,
				SerializerFeature.DisableCircularReferenceDetect
				);
		return fastJsonConfig;
	}
	

	
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/index.html").addResourceLocations("/index.html");
		registry.addResourceHandler("/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
		registry.addResourceHandler("/img/**").addResourceLocations("/img/");
		registry.addResourceHandler("/libs/**").addResourceLocations("/libs/");
		registry.addResourceHandler("/modular/**").addResourceLocations("/modular/");
	}

	

	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		JsonHttpMessageConverter JsonHttpMessageConverter = new JsonHttpMessageConverter();
		JsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig());
		converters.add(JsonHttpMessageConverter);
	}

	

	public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
		ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
		resolver.setContentNegotiationManager(manager);
		List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setViewClass(JstlView.class);
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		resolvers.add(viewResolver);
		resolver.setViewResolvers(resolvers);
		return resolver;
	}
	
	@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
	
}
